package com.hadoop.fof;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class FReducer extends Reducer<Text, IntWritable,Text, Text> {

    Text rvalue = new Text();

    protected  void reduce(Text key,Iterable<IntWritable> values,Reducer<Text,IntWritable,Text,Text>.Context context) throws IOException, InterruptedException {
            int sum = 0;
            int flag = 0;
            //相同的 key 为一组，调用一次 reduce
            for(IntWritable i : values){
                sum += i.get();
                //如果是直接好友， 不再计算
                if(i.get() == 0){
                    flag = 1;
                    break;
                }
            }
            if(flag == 0){
                rvalue.set(sum + "");
                context.write(key,rvalue);
            }
    }
}
